package top.crxk.consumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import top.crxk.common.entity.CommonResult;
import top.crxk.common.entity.Payment;
import top.crxk.consumer.service.PaymentService;

import javax.annotation.Resource;

/**
 * [springcloud]
 * [top.crxk.consumer.controller]
 * [2020/11/4 4:42 下午]
 *
 * @author [crxk]
 * @version 1.0.0
 */
@RestController
@Slf4j
public class OrderController {
    /**这里的服务名称要从服务注册中心（nacos eureka）里面粘贴过来，注意大小写**/
    private static final String PAYMENT_URL = "http://payment-service";
    private final PaymentService paymentService;
    private final RestTemplate restTemplate;

    @Autowired
    public OrderController(PaymentService paymentService, RestTemplate restTemplate) {
        this.paymentService = paymentService;
        this.restTemplate = restTemplate;
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping("/feign/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentByFeign(@PathVariable("id") Long id) {
        return paymentService.getPaymentById(id);
    }
}
